<template>
	<a-card :bordered="false" style="width: 100%">
		<a-form ref="searchFormRef" :model="searchFormState">
			<a-row :gutter="10">
				<a-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
					<a-form-item label="比赛名称" name="matchName">
						<a-input v-model:value="searchFormState.matchName" placeholder="请输入比赛名称" />
					</a-form-item>
				</a-col>
				<a-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
					<a-form-item label="比赛类型" name="matchType">
						<a-select v-model:value="searchFormState.matchType" placeholder="请选择比赛类型" :options="matchTypeOptions" />
					</a-form-item>
				</a-col>
				<a-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
					<a-form-item label="比赛状态" name="matchStatus">
						<a-select v-model:value="searchFormState.matchStatus" placeholder="请选择比赛状态" :options="matchStatusOptions" />
					</a-form-item>
				</a-col>
				<a-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
				    <a-form-item>
				        <a-space>
				            <a-button type="primary" @click="tableRef.refresh(true)">
				                <template #icon><SearchOutlined /></template>
				                查询
				            </a-button>
				            <a-button @click="reset">
				                <template #icon><redo-outlined /></template>
				                 重置
				            </a-button>
                        </a-space>
                    </a-form-item>
                </a-col>
            </a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
			:scroll="{ x: 'max-content' }"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('matchAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button @click="importModelRef.onOpen()" v-if="hasPerm('matchImport')">
                        <template #icon><import-outlined /></template>
                        <span>导入</span>
                    </a-button>
                    <a-button @click="exportData" v-if="hasPerm('matchExport')">
                        <template #icon><export-outlined /></template>
                        <span>导出</span>
                    </a-button>
					<xn-batch-button
						v-if="hasPerm('matchBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						buttonDanger
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchMatch"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'matchType'">
					{{ $TOOL.dictTypeData('match_type', record.matchType) }}
				</template>
				<template v-if="column.dataIndex === 'matchStatus'">
					{{ $TOOL.dictTypeData('match_status', record.matchStatus) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="reportMatch(record)" v-if="hasPerm('matchReport')">报名</a>
						<a @click="viewPlayers(record)" v-if="hasPerm('matchReport')">查看</a>
						<a @click="openGrouping(record)" v-if="hasPerm('matchGroup')">分组</a>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('matchEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['matchEdit', 'matchDelete','matchReport'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteMatch(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('matchDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<ImportModel ref="importModelRef" />
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<PlayerListModal ref="playerListModalRef" />
</template>

<script setup name="match">
import tool from '@/utils/tool'
import { cloneDeep } from 'lodash-es'
import { useRouter } from 'vue-router'
	import Form from './form.vue'
	import ImportModel from './importModel.vue'
	import PlayerListModal from './playerListModal.vue'
	import downloadUtil from '@/utils/downloadUtil'
	import matchApi from '@/api/dota/matchApi'
	import matchPlayerApi from '@/api/dota/matchPlayerApi'
const router = useRouter()
const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const importModelRef = ref()
	const formRef = ref()
	const playerListModalRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '比赛名称',
			dataIndex: 'matchName'
		},
		{
			title: '比赛描述',
			dataIndex: 'matchDesc'
		},
		{
			title: '比赛开始日期',
			dataIndex: 'matchStartDate'
		},
		{
			title: '比赛结束日期',
			dataIndex: 'matchEndDate'
		},
		{
			title: '比赛类型',
			dataIndex: 'matchType'
		},
		{
			title: '比赛状态',
			dataIndex: 'matchStatus'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['matchEdit', 'matchDelete','matchReport','matchGroup'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			fixed: 'right'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return matchApi.matchPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteMatch = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		matchApi.matchDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 导出
    const exportData = () => {
        if (selectedRowKeys.value.length > 0) {
            const params = selectedRowKeys.value.map((m) => {
                return {
                    id: m
                }
            })
            matchApi.matchExport(params).then((res) => {
                downloadUtil.resultDownload(res)
            })
        } else {
            matchApi.matchExport([]).then((res) => {
                downloadUtil.resultDownload(res)
            })
        }
    }
	// 批量删除
	const deleteBatchMatch = (params) => {
		matchApi.matchDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}

	const reportMatch = (record) => {
		console.log(record)
		matchPlayerApi.matchPlayerSubmitForm({
			matchId: record.id,
		}).then(() => {
			tableRef.value.refresh(true)
		})
	}

	// 查看报名选手
	const viewPlayers = (record) => {
		playerListModalRef.value.onOpen(record)
	}

	const openGrouping = (record) => {
		const routeUrl = router.resolve({
			path: '/dota/grouping',
			query: {
				matchId: record.id,
				matchName: record.matchName
			}
		})
		window.open(routeUrl.href, '_blank')
	}

	const matchTypeOptions = tool.dictList('match_type')
	const matchStatusOptions = tool.dictList('match_status')
</script>
